<div class="layui-layout layui-layout-admin" style="padding-left: 20px;">
    <div class="layui-row" style="margin-top: 20px;">
        <div class="layui-col-xs6">
            <div class="layui-btn-group">
                <button class="layui-btn" data-type="tabAdd" id="add">新增</button>
                <button class="layui-btn batch" data-type="batchstart" >启动</button>
                <button class="layui-btn layui-btn-danger batch" data-type="batchpause" >暂停</button>cron
            </div>
        </div>
        <div class="layui-col-xs6 search_text" style="text-align: right;">

            <form class="layui-form" action="" onsubmit="javascript:return false;">
                <div class="demoTable">
                    <div class="layui-inline" style="width: 40%">
                        <input class="layui-input" name="task_name" id="task_name" autocomplete="off"  placeholder="任务名称" >
                    </div>
                    <div class="layui-inline pp-search" style="width: 20%;text-align: left;">
                        <select name="modules" lay-verify="required" lay-filter="select_group" id="group_id" lay-search="">
                            <option value="0">全部</option>
                            {{range $k, $v := .taskGroup}}
                                <option value="{{$k}}" {{if eq $k $.groupId}} selected {{end}}>{{$v}}</option>
                            {{end}}
                        </select>
                    </div>
                    <button class="layui-btn" data-type="reload" id="reload"><i class="fa fa-search"></i>查询</button>
                </div>
            </form>
        </div>
    </div>

    <table class="layui-hide" id="table_list" lay-filter="table_filter">
    </table>

    <script type="text/html" id="bar">
        {{/*<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="status">启|停</a>*/}}
        {{/*<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>*/}}
        <a class="layui-btn layui-btn-xs " lay-event="detail">详细</a>
        <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="run">测试</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="log">日志</a>
    </script>
</div>
<script>

    layui.use(['table','form','element'], function(){
        var table = layui.table;
        var form = layui.form;
        var element = layui.element;
        var error_info = "{{.flash.error}}";
        if(error_info){
            layer.msg(error_info,{icon: 2,shade:0.3},function () {
                window.history.go(-1)
            })
            return;
        }
        //方法级渲染
        table.render({
            elem: '#table_list'
            ,url: '/task/table'
            ,cols: [[
                {checkbox: true, fixed: true},
                {field:'id', title: 'ID', align:'center', width:80,sort: true}
                ,{field:'task_name', title: '任务名称'}
                ,{field:'cron_spec',title: '时间表达式', width:100}
                ,{field:'next_time', width:170,title: '下次执行时间'}
                ,{field:'pre_time', title: '上次执行时间',width:170,}
                ,{field:'execute_times', title: '次数',width:100}
                ,{width:180, align:'center', title:'操作', toolbar: '#bar'}
            ]]
            ,id: 'listReload'
            ,page: true
            ,limit:20
            ,limits: [20,40,80]
            ,height: "full-130"
        });

        var $ = layui.$, active = {
            reload: function(){
                table.reload('listReload', {
                    where: {
                        group_id: $('#group_id').val(),
                        task_name:$('#task_name').val(),
                    },
                    page: 1
                });
            }
        };

        $("#add").on("click",function() {
            window.parent.openTab("/task/add",'新增任务',"admin_task_add-0",'fa-plus');
        })

        // form.on('select(select_group)', function(data){
        //     $("#reload").click()
        //     form.render('select'); //刷新select选择框渲染
        // });

        //监听工具条
        table.on('tool(table_filter)', function(obj){
            var data = obj.data;
            var task_name = window.parent.delHtmlTag(data.task_name)
            task_name = window.parent.getDelimiterLastString(task_name,"-")
            if(obj.event === 'edit'){
                window.parent.openTab("/task/edit?id="+data.id,task_name+' 编辑',"admin_task_edit-"+data.id,'fa-edit');
            } else if(obj.event === 'log'){
                window.parent.openTab("/tasklog/list?task_id="+data.id,task_name+'日志',"admin_log-"+data.id,'');
            }else if (obj.event==='detail'){
                window.parent.openTab('/task/detail?id='+data.id,task_name+'详情',"admin_task_detail-"+data.id,'fa-folder-open-o');
            }else if (obj.event==='run'){
                layer.confirm("测试执行任务，注意不要超时，请确认执行", {icon: 3, title:'提示'}, function(index){
                    layer.load();
                    $.post('/task/ajaxrun', {id:data.id}, function (out) {
                        if (out.status == 0) {
                            layer.msg("操作成功",{icon: 1,shade:0.3,time:1000},function () {
                                window.parent.openTab("/tasklog/list?task_id="+data.id,task_name+'运行日志',"admin_task_log-"+data.id,'');
                            })
                        } else {
                            layer.msg(out.message)
                            return
                        }
                    }, "json");
                    setTimeout(function(){
                        layer.closeAll('loading');
                    }, 2000);
                    layer.close(index);
                });
            }else if (obj.event==='status'){
                layer.msg("状态")
            }else{
                layer.msg('操作不存在')
            }
        });
        
        $(".batch").on('click',function (obj) {
            var action = $(this).attr('data-type');
            var checkStatus = table.checkStatus('listReload');
            var data = checkStatus.data;
            if(data.length<1){
                layer.msg("请选择操作数据");
                return;
            }

            var  msg = "确认启动这些任务吗";
            if (action=='batchpause'){
                msg = "确定暂定这些任务吗";
            }

            layer.confirm(msg, {icon: 3, title:'提示'}, function(index){
                var ids = "";
                $.each(data,function (k,v) {
                    ids += v.id+",";
                });
                ids = ids.substr(0,ids.length-1);

                layer.load();
                $.post('/task/ajax'+action, {"ids":ids}, function (out) {
                    if (out.status == 0) {
                        layer.msg("操作成功",{icon: 1,shade:0.3,time:1000},function () {
                            $('#reload').click();
                        })
                    } else {
                        layer.msg(out.message)
                        return
                    }
                }, "json");
                setTimeout(function(){
                    layer.closeAll('loading');
                }, 2000);


                layer.close(index);
            });
            return ;
        })

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

</script>